﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Windows.Browser;
using System.Collections.Generic;

namespace Jzy.Workflow.Code
{
    public class FlowContextMenu
    {
        private ContextMenuType _type;
        private int _pkid;
        private Canvas _canvas;
        private Point _position;
        private Border _border;
        private Grid _grid ;
        private List<Menu.MenuItem> _items;
        

        private static FlowContextMenu _menu;

        public event RoutedEventHandler Updated;

        public static FlowContextMenu Create(ContextMenuType type, int pkid, Canvas canvas, Point position)
        {
            if (_menu == null)
            {
                _menu = new FlowContextMenu(type, pkid,canvas,position);
            }
            else
            {
                _menu._type = type;
                _menu._pkid = pkid;
                _menu._canvas = canvas;
                _menu._position = position;
            }

            return _menu;
        }

        public static void Dispose()
        {
            if (_menu != null)
            {
                _menu._canvas.Children.Remove(_menu._border);
                _menu = null;
            }
        }
     
        private FlowContextMenu(ContextMenuType type, int pkid, Canvas canvas, Point position)
        {
            this._type = type;
            this._pkid = pkid;
            this._canvas = canvas;
            this._position = position;

            _border = new Border();
            _border.BorderBrush = new SolidColorBrush(Color.FromArgb(0xff, 0x8a, 0x86, 0x7a));
            _border.BorderThickness = new Thickness(1);
            _border.Padding = new Thickness(0, 2, 0, 2);
            _border.Width = 120;
            _border.Background = new SolidColorBrush(Colors.White);

            _grid = new Grid();
            ImageBrush bg = new ImageBrush();
            bg.ImageSource = new BitmapImage(new Uri("images/menu_bg.png", UriKind.Relative));
            _grid.Background = bg;
            ColumnDefinition col = new ColumnDefinition();
            col.Width = new GridLength(30);
            _grid.ColumnDefinitions.Add(col);
            col = new ColumnDefinition();
            _grid.ColumnDefinitions.Add(col);

            _items = new List<Jzy.Workflow.Code.Menu.MenuItem>();

            _canvas.Children.Add(_border);
        }

        public void Render()
        {
            _items.Clear();
            switch (_type)
            {
                case ContextMenuType.Flow:
                    _items.Add(new Jzy.Workflow.Code.Menu.MenuItem("增加节点"));
                    _items.Add(new Jzy.Workflow.Code.Menu.MenuItem("增加路径"));
                    _items.Add(new Jzy.Workflow.Code.Menu.MenuItem("属性设置"));
                    break;
                case ContextMenuType.Node:
                    _items.Add(new Jzy.Workflow.Code.Menu.MenuItem("修改节点"));
                    _items.Add(new Jzy.Workflow.Code.Menu.MenuItem("删除节点"));
                    break;
                case ContextMenuType.Line:
                    _items.Add(new Jzy.Workflow.Code.Menu.MenuItem("修改路径"));
                    _items.Add(new Jzy.Workflow.Code.Menu.MenuItem("删除路径"));
                    break;
            }

            Canvas.SetZIndex(_border, GlobalVar.ZIndex);
            _grid.RowDefinitions.Clear();
            for (int i = 0; i < _items.Count; i++)
            {
                RowDefinition row = new RowDefinition();
                row.Height = new GridLength(22);
                _grid.RowDefinitions.Add(row);
            }

            _border.Child = _grid;
            _grid.Children.Clear();

            for (int i = 0; i < _items.Count; i++)
            {
                _items[i].Render(_grid, i);
                _items[i].Click += new RoutedEventHandler(FlowContextMenu_Click);
            }

            Canvas.SetLeft(_border,_position.X);
            Canvas.SetTop(_border,_position.Y);            
        }

        void FlowContextMenu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Menu.MenuItem item = (Menu.MenuItem)sender;
                string text = item.Text;
                FlowContextMenu.Dispose();
                object flag = null;
                switch (text)
                {
                    case "增加节点":
                        flag = JSUtil.ShowNode(_pkid, null);
                        break;
                    case "增加路径":
                        flag = JSUtil.ShowLine(_pkid, null);
                        break;
                    case "属性设置":
                        flag = JSUtil.ShowFlow(_pkid);
                        break;
                    case "修改节点":
                        flag = JSUtil.ShowNode(null, _pkid);
                        break;
                    case "删除节点":
                        {
                            bool result = HtmlPage.Window.Confirm("确认删除此节点");
                            if (result)
                            {
                                DataService.FlowSoapClient client = DataFactory.Create();
                                client.DeleteNodeCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_DeleteCompleted);
                                client.DeleteNodeAsync(_pkid);
                            }
                        }
                        break;
                    case "修改路径":
                        flag = JSUtil.ShowLine(null, _pkid);
                        break;
                    case "删除路径":
                        {
                            bool result = HtmlPage.Window.Confirm("确认删除此路径");
                            if (result)
                            {
                                DataService.FlowSoapClient client = DataFactory.Create();
                                client.DeleteLineCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_DeleteCompleted);
                                client.DeleteLineAsync(_pkid);
                            }
                        }
                        break;
                }
                if (flag != null)
                {
                    Updated(sender, e);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void client_DeleteCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            
            Updated(sender, null);
        }


    }
}
